﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using BBMS.DLL.Base;
using MySql.Data.MySqlClient;
namespace BBMS.DLL.DataContract
{

    public class HoaDonNhap : ChungTuBase
    {
        public HoaDonNhap()
        {
            this.LoaiCT = "HDN";
            this.IsChi = true;
        }
        //public ArrayList ChungTuList;
        public DiscountList DiscountList;
        public HoaDonNhap(long id, string maCT, string soCT,
             DateTime ngayCT, DateTime ngayHT, DateTime ngayTTDuKien,
             DateTime ngayTT, TrangThaiThanhToan trangThaiTT,
             string maSoHS, string maNCC, string maQuay,
             long giatriHangHoa, long chietKhau,
             long giaTriChuaThue, int thueSuat, long giaTriThue,
             long giaTriCT, long giaTriDaTT, string ghiChu,
             int nvID)
            : base(id, "HDN", maCT, soCT, ngayCT, ngayHT, ngayTTDuKien, ngayTT, trangThaiTT, maSoHS,
                maNCC, maQuay, "", giatriHangHoa, chietKhau, giaTriChuaThue, thueSuat, giaTriThue, giaTriCT,
                giaTriDaTT, "", false, true, ghiChu, nvID)
        {
            this.LoaiCT = "HDN";
            this.IsChi = true;
            this.DiscountList = new DiscountList(ID);

        }

        public override System.Data.DataTable GetObjectDataTable()
        {
            System.Data.DataTable dt = new System.Data.DataTable(this.TableName);

            dt.Columns.Add("ID", typeof(String));
            dt.Columns.Add("MaCT", typeof(String));
            dt.Columns.Add("SoCT", typeof(String));
            dt.Columns.Add("LoaiCT", typeof(String));
            dt.Columns.Add("NgayCT", typeof(DateTime));
            dt.Columns.Add("NgayHT", typeof(DateTime));
            dt.Columns.Add("NgayTTDuKien", typeof(DateTime));
            dt.Columns.Add("NgayTT", typeof(DateTime));
            dt.Columns.Add("TrangThaiTT", typeof(int));
            dt.Columns.Add("MaHoSo", typeof(String));
            dt.Columns.Add("MaNCC", typeof(String));
            dt.Columns.Add("MaQuay", typeof(String));

            dt.Columns.Add("GiaTriHangHoa", typeof(long));
            dt.Columns.Add("ChietKhau", typeof(long));
            dt.Columns.Add("GiaTriChuaThue", typeof(long));
            dt.Columns.Add("ThueSuat", typeof(int));
            dt.Columns.Add("GiaTriThue", typeof(long));
            dt.Columns.Add("GiaTriCT", typeof(long));
            dt.Columns.Add("GiaTriDaTT", typeof(long));

            dt.Columns.Add("GhiChu", typeof(String));
            dt.Columns.Add("IsChi", typeof(bool));

            dt.Columns.Add("NVID", typeof(int));

            return dt;
        }
        public override System.Data.DataRow ObjectToDataRow()
        {
            System.Data.DataRow row = this.GetObjectDataTable().NewRow();
            row["ID"] = this.ID;
            row["MaCT"] = this.MaCT;
            row["SoCT"] = this.SoCT;
            row["LoaiCT"] = "HDN";
            row["NgayCT"] = this.NgayCT;
            row["NgayHT"] = this.NgayHT;
            row["NgayTTDuKien"] = this.NgayTTDuKien;
            row["NgayTT"] = this.NgayTT;
            row["TrangThaiTT"] = (int)this.TrangThaiTT;
            row["MaHoSo"] = this.MaHoSo;
            row["MaNCC"] = this.MaNCC;
            row["MaQuay"] = this.MaQuay;

            row["GiaTriHangHoa"] = this.GiaTriHangHoa;
            row["ChietKhau"] = this.ChietKhau;
            row["GiaTriChuaThue"] = this.GiaTriChuaThue;
            row["ThueSuat"] = this.ThueSuat;
            row["GiaTriThue"] = this.GiaTriThue;
            row["GiaTriCT"] = this.GiaTriCT;
            row["GiaTriDaTT"] = this.GiaTriDaTT;

            row["GhiChu"] = this.GhiChu;
            row["IsChi"] = true;

            row["NVID"] = this.NVID;

            return row;
        }

        public override void UpdateInsertedID(MySql.Data.MySqlClient.MySqlCommand cmd)
        {
            cmd.CommandText = "SELECT LAST_INSERT_ID()";
            this.ID = CommonTools.Helper.StringToLong(cmd.ExecuteScalar().ToString());
            if (DiscountList != null) DiscountList = new DiscountList(ID);
            else DiscountList.ImportID = ID;
        }
        public override bool CanDelete(MySql.Data.MySqlClient.MySqlCommand cmd, System.Data.DataRow row)
        {
            return false;
        }
        public override BBMS.DLL.Base.ObjectBase DataRowToObject(System.Data.DataRow row)
        {
            HoaDonNhap obj = null;
            try
            {
                obj = new HoaDonNhap(
                    CommonTools.Helper.StringToLong(row["ID"].ToString()),
                    row["MaCT"].ToString(),
                    row["SoCT"].ToString(),
                    (DateTime)row["NgayCT"],
                    (DateTime)row["NgayHT"],
                    (DateTime)row["NgayTTDuKien"],
                    (DateTime)row["NgayTT"],
                    (TrangThaiThanhToan)(CommonTools.Helper.StringToInt(row["TrangThaiTT"].ToString())),
                    row["MaHoSo"].ToString(),
                    row["MaNCC"].ToString(),
                    row["MaQuay"].ToString(),
                    CommonTools.Helper.StringToLong(row["GiaTriHangHoa"].ToString()),
                    CommonTools.Helper.StringToLong(row["ChietKhau"].ToString()),
                    CommonTools.Helper.StringToLong(row["GiaTriChuaThue"].ToString()),
                    CommonTools.Helper.StringToInt(row["ThueSuat"].ToString()),
                    CommonTools.Helper.StringToLong(row["GiaTriThue"].ToString()),
                    CommonTools.Helper.StringToLong(row["GiaTriCT"].ToString()),
                    CommonTools.Helper.StringToLong(row["GiaTriDaTT"].ToString()),
                    row["GhiChu"].ToString(),
                    CommonTools.Helper.StringToInt(row["NVID"].ToString()));
            }
            catch { }
            return obj;
        }

        /*
        public void LoadChungTuList()
        {
            if (ChungTuList != null) ChungTuList.Clear();
            else ChungTuList = new ArrayList();

            try
            {
                MySql.Data.MySqlClient.MySqlConnection con = new MySql.Data.MySqlClient.MySqlConnection(Config.ConnectionString());
                con.Open();
                MySqlCommand cmd = con.CreateCommand();
                cmd.CommandText = "select d.CTTHU_ID, d.GiaTriHT, b.MaCT, b.SoCT, b.NgayCT, b.LoaiCT, b.GiaTriCT, b.GiaTriDaTT, b.TrangThaiTT from discount d inner join bill b where d.HDN_ID = @HDN_ID and d.CTTHU_ID = b.ID";
                cmd.Parameters.Add("@HDN_ID", this.ID);
                MySqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    Discount obj = new Discount(CommonTools.Helper.StringToLong(rd["CTTHU_ID"].ToString()),
                        //rd["MaCT"].ToString(),
                        //rd["SoCT"].ToString(),
                        (DateTime)rd["NgayCT"],
                        rd["LoaiCT"].ToString(),
                        CommonTools.Helper.StringToLong(rd["GiaTriCT"].ToString()),
                        CommonTools.Helper.StringToLong(rd["GiaTriDaTT"].ToString()),
                        0,
                        CommonTools.Helper.StringToLong(rd["GiaTriHT"].ToString()), true,
                        (TrangThaiThanhToan)CommonTools.Helper.StringToInt(rd["TrangThaiTT"].ToString()));
                    ChungTuList.Add(obj);
                }
                rd.Close();
                rd.Dispose();
                con.Close();
                con.Dispose();
            }
            catch {
                ChungTuList.Clear();
            }
        }*/
        public override string InsertNew()
        {
             string msg = "";
             try
             {
                 MySql.Data.MySqlClient.MySqlConnection con = new MySql.Data.MySqlClient.MySqlConnection(Config.ConnectionString());
                 msg = "Lỗi: Không thể mở kết nối tới cơ sở dữ liệu.";
                 con.Open();
                 msg = "";
                 MySql.Data.MySqlClient.MySqlCommand cmd = con.CreateCommand();
                 MySql.Data.MySqlClient.MySqlTransaction tran = con.BeginTransaction();
                 cmd.Transaction = tran;
                 try
                 {
                     msg = base.InsertNew(cmd);
                     if (msg == "")
                     {
                         if (DiscountList != null) DiscountList.Insert2DB(cmd);
                         tran.Commit();
                     }
                     else tran.Rollback(); // insert failed => rollback
                 }
                 catch
                 {
                     msg = "Lỗi: Lỗi xảy ra trong quá trình xử lý dữ liệu.";
                     tran.Rollback();
                 }
                 con.Clone();
                 con.Dispose();
             }
             catch {
                 msg = "Lỗi: Lỗi xảy ra trong quá trình xử lý dữ liệu.";
             }
             return msg;
        }

        public override string Save()
        {
            string msg = "";
            try
            {
                MySql.Data.MySqlClient.MySqlConnection con = new MySql.Data.MySqlClient.MySqlConnection(Config.ConnectionString());
                msg = "Lỗi: Không thể mở kết nối tới cơ sở dữ liệu.";
                con.Open();
                msg = "";
                MySql.Data.MySqlClient.MySqlCommand cmd = con.CreateCommand();
                MySql.Data.MySqlClient.MySqlTransaction tran = con.BeginTransaction();
                cmd.Transaction = tran;
                try
                {
                    msg = base.Save(cmd);
                    if (msg == "" && DiscountList != null)
                    {
                        DiscountList.Update2DB(cmd);
                        tran.Commit();
                    }
                    else tran.Rollback(); // save failed => rollback
                }
                catch
                {
                    msg = "Lỗi: Lỗi xảy ra trong quá trình xử lý dữ liệu.";
                    tran.Rollback();
                }
                con.Clone();
                con.Dispose();
            }
            catch
            {
                msg = "Lỗi: Lỗi xảy ra trong quá trình xử lý dữ liệu.";
            }
            return msg;
        }
        public override BBMS.DLL.Base.ObjectBase DataReaderToObject(MySql.Data.MySqlClient.MySqlDataReader row)
        {
            HoaDonNhap obj = null;
            try
            {
                obj = new HoaDonNhap(
                    CommonTools.Helper.StringToLong(row["ID"].ToString()),
                    row["MaCT"].ToString(),
                    row["SoCT"].ToString(),
                    (DateTime)row["NgayCT"],
                    (DateTime)row["NgayHT"],
                    (DateTime)row["NgayTTDuKien"],
                    (DateTime)row["NgayTT"],
                    (TrangThaiThanhToan)(CommonTools.Helper.StringToInt(row["TrangThaiTT"].ToString())),
                    row["MaHoSo"].ToString(),
                    row["MaNCC"].ToString(),
                    row["MaQuay"].ToString(),
                    CommonTools.Helper.StringToLong(row["GiaTriHangHoa"].ToString()),
                    CommonTools.Helper.StringToLong(row["ChietKhau"].ToString()),
                    CommonTools.Helper.StringToLong(row["GiaTriChuaThue"].ToString()),
                    CommonTools.Helper.StringToInt(row["ThueSuat"].ToString()),
                    CommonTools.Helper.StringToLong(row["GiaTriThue"].ToString()),
                    CommonTools.Helper.StringToLong(row["GiaTriCT"].ToString()),
                    CommonTools.Helper.StringToLong(row["GiaTriDaTT"].ToString()),
                    row["GhiChu"].ToString(),
                    CommonTools.Helper.StringToInt(row["NVID"].ToString()));
            }
            catch { }
            return obj;
        }
    }


    /*public class HoaDonNhap : BBMS.DLL.Base.ObjectBase
    {
        public long ID;
        public string MaCT;
        public string SoCT;
        public string LoaiCT = "HDN";
        public DateTime NgayCT;
        public DateTime NgayHT; // ngay hoach toan
        public DateTime NgayTT;
        public DateTime NgayTTDuKien;
        public string MaNCC;
        public string MaQuay;
        public string MaHoSo;
        public long GiaTriGoc;
        public long ChietKhau;
        public long GiaTri;
        public int ThueSuat;
        public long GiaTriThue;
        public string GhiChu;
        public bool IsChi = true;
        public TrangThaiThanhToan TrangThaiTT = TrangThaiThanhToan.ChuaTT;
        public int NVID = 0;

        public HoaDonNhap() 
        {
        }

        public HoaDonNhap(long id, string maCT, string soCT, 
            DateTime ngayCT, DateTime ngayHT, DateTime ngayTTDuKien, DateTime ngayTT, string maHoSo,
            string maNCC, string maQuay,
            long giatrigoc, long chietkhau, long giatri,
            int thuesuat, long giatrithue, string ghiChu, TrangThaiThanhToan trangThaiTT, int nvid)
        {
            this.ID = id;
            this.MaCT = maCT;
            this.SoCT = soCT;
            this.NgayCT = ngayCT;
            this.NgayHT = ngayHT;
            this.NgayTT = ngayTT;
            this.NgayTTDuKien = ngayTTDuKien;
            this.MaHoSo = maHoSo; 
            this.MaNCC = maNCC;
            this.MaQuay = maQuay;
            this.GiaTriGoc = giatrigoc;
            this.ChietKhau = chietkhau;
            this.GiaTri = giatri;
            this.ThueSuat = thuesuat;
            this.GiaTriThue = giatrithue;
            this.GhiChu = ghiChu;
            this.TrangThaiTT = trangThaiTT;
            this.NVID = nvid;
        }

        public override object Key
        {
            get
            {
                return this.ID;
            }
        }
        public override string Name
        {
            get
            {
                return this.MaCT + "-" + this.SoCT;
            }
        }
        public override string  TableName
        {
            get 
            {
                return "bill";
            }
        }
        
        public override string[] KeyFields
        {
            get
            {
                return new string[] { "MaCT", "SoCT" };
            }
        }

        public override string[]  IDFields
        {
            get
            {
                return new string[] { "ID" };
            }
        }
        public override string ExWhereClause
        {
            get
            {
                return " LoaiCT = 'HDN' ";
            }
        }
        public override System.Data.DataTable GetObjectDataTable()
        {
            System.Data.DataTable dt = new System.Data.DataTable(this.TableName);
            dt.Columns.Add("ID", typeof(String));
            dt.Columns.Add("MaCT", typeof(String));
            dt.Columns.Add("SoCT", typeof(String));
            dt.Columns.Add("LoaiCT", typeof(String));
            dt.Columns.Add("NgayCT", typeof(DateTime));
            dt.Columns.Add("NgayHT", typeof(DateTime));
            dt.Columns.Add("NgayTTDuKien", typeof(DateTime));
            dt.Columns.Add("NgayTT", typeof(DateTime));
            dt.Columns.Add("MaHoSo", typeof(String));
            dt.Columns.Add("MaNCC", typeof(String));
            dt.Columns.Add("MaQuay", typeof(String));
            dt.Columns.Add("GiaTriGoc", typeof(long));
            dt.Columns.Add("ChietKhau", typeof(long));
            dt.Columns.Add("GiaTri", typeof(long));
            dt.Columns.Add("ThueSuat", typeof(int));
            dt.Columns.Add("GiaTriThue", typeof(long));
            dt.Columns.Add("GhiChu", typeof(String));
            dt.Columns.Add("TrangThaiTT", typeof(int));
            dt.Columns.Add("IsChi", typeof(bool));
            dt.Columns.Add("NVID", typeof(int));

            return dt;
        }
        public override System.Data.DataRow ObjectToDataRow()
        {
            System.Data.DataRow row = this.GetObjectDataTable().NewRow();
            row["ID"] = this.ID;
            row["MaCT"] = this.MaCT;
            row["SoCT"] = this.SoCT;
            row["LoaiCT"] = this.LoaiCT;
            row["NgayCT"] = this.NgayCT;
            row["NgayHT"] = this.NgayHT;
            row["NgayTTDuKien"] = this.NgayTTDuKien;
            row["NgayTT"] = this.NgayTT;
            row["MaHoSo"] = this.MaHoSo;
            row["MaNCC"] = this.MaNCC;
            row["MaQuay"] = this.MaQuay;
            row["GiaTriGoc"] = this.GiaTriGoc;
            row["ChietKhau"] = this.GiaTriThue;
            row["GiaTri"] = this.GiaTri;
            row["ThueSuat"] = this.ThueSuat;
            row["GiaTriThue"] = this.GiaTriThue;
            row["GhiChu"] = this.GhiChu;
            row["TrangThaiTT"] = (int)this.TrangThaiTT;
            row["IsChi"] = this.IsChi;
            row["NVID"] = this.NVID;
            return row;
        }
        
        public override void UpdateInsertedID(MySql.Data.MySqlClient.MySqlCommand cmd)
        {
            cmd.CommandText = "SELECT LAST_INSERT_ID()";
            this.ID = CommonTools.Helper.StringToLong(cmd.ExecuteScalar().ToString());
        }
        public override bool CanDelete(MySql.Data.MySqlClient.MySqlCommand cmd, System.Data.DataRow row)
        {
            return true;
        }
        public override BBMS.DLL.Base.ObjectBase DataRowToObject(System.Data.DataRow row)
        {
            HoaDonNhap obj = null;
            try
            {
                obj = new HoaDonNhap(CommonTools.Helper.StringToLong(row["ID"].ToString()),
                    row["MaCT"].ToString(),
                    row["SoCT"].ToString(),
                    (DateTime)row["NgayCT"],
                    (DateTime)row["NgayHT"],
                    (DateTime)row["NgayTTDuKien"],
                    (DateTime)row["NgayTT"],
                    row["MaHoSo"].ToString(),
                    row["MaNCC"].ToString(),
                    row["MaQuay"].ToString(),
                    CommonTools.Helper.StringToLong(row["GiaTriGoc"].ToString()),
                    CommonTools.Helper.StringToLong(row["ChietKhau"].ToString()),
                    CommonTools.Helper.StringToLong(row["GiaTri"].ToString()),
                    CommonTools.Helper.StringToInt(row["ThueSuat"].ToString()),
                    CommonTools.Helper.StringToLong(row["GiaTriThue"].ToString()),
                    row["GhiChu"].ToString(),
                    (TrangThaiThanhToan)(CommonTools.Helper.StringToInt(row["TrangThaiTT"].ToString())),
                    CommonTools.Helper.StringToInt(row["NVID"].ToString()));
            }
            catch { }
            return obj;
        }

        public override BBMS.DLL.Base.ObjectBase DataReaderToObject(MySql.Data.MySqlClient.MySqlDataReader rd)
        {
            HoaDonNhap obj = null;
            try
            {
                obj = new HoaDonNhap(CommonTools.Helper.StringToLong(rd["ID"].ToString()),
                    rd["MaCT"].ToString(),
                    rd["SoCT"].ToString(),
                    (DateTime)rd["NgayCT"],
                    (DateTime)rd["NgayHT"],
                    (DateTime)rd["NgayTTDuKien"],
                    (DateTime)rd["NgayTT"],
                    rd["MaHoSo"].ToString(),
                    rd["MaNCC"].ToString(),
                    rd["MaQuay"].ToString(),
                    CommonTools.Helper.StringToLong(rd["GiaTriGoc"].ToString()),
                    CommonTools.Helper.StringToLong(rd["ChietKhau"].ToString()),
                    CommonTools.Helper.StringToLong(rd["GiaTri"].ToString()),
                    CommonTools.Helper.StringToInt(rd["ThueSuat"].ToString()),
                    CommonTools.Helper.StringToLong(rd["GiaTriThue"].ToString()),
                    rd["GhiChu"].ToString(),
                    (TrangThaiThanhToan)(CommonTools.Helper.StringToInt(rd["TrangThaiTT"].ToString())),
                    CommonTools.Helper.StringToInt(rd["NVID"].ToString()));
            }
            catch { }
            return obj;
        }
    }*/
}
